package cn.xlbweb.cli.controller;

import cn.xlbweb.cli.common.ServerResponse;
import cn.xlbweb.cli.config.CliOperateLog;
import cn.xlbweb.cli.common.constant.OperationConsts;
import cn.xlbweb.cli.pojo.dto.LoginDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author: bobi
 * @date: 2019-02-04 00:55
 * @description:
 */
@RestController
public class LoginController {

    @CliOperateLog(module = "用户模块", type = OperationConsts.LOGIN, description = "使用账号+密码进行登录操作")
    @PostMapping("login")
    public ServerResponse login(@Valid LoginDTO loginDTO) {
        UsernamePasswordToken token = new UsernamePasswordToken(loginDTO.getUsername(), loginDTO.getPassword());
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        return ServerResponse.success("登陆成功", subject.getSession().getId());
    }

    @GetMapping("logout")
    public ServerResponse logout() {
        SecurityUtils.getSubject().logout();
        return ServerResponse.success("退出成功");
    }
}
